﻿<?php
require("admin-header.php");
if (!(isset($_SESSION['administrator']))) 
{
    echo "<a href='../loginpage.php'>Please Login First!</a>";
    exit(1);
}
?>
<?php
if (isset($_POST['prefix'])) 
{
    require_once("../include/check_post_key.php");
    $prefix = $_POST['prefix'];
    require_once("../include/my_func.inc.php");
    if (!is_valid_user_name($prefix)) 
    {
        echo "Prefix is not valid.";
        exit(0);
    }
    $teamnumber = intval($_POST['teamnumber']);
    $pieces     = explode("\n", trim($_POST['ulist']));
    
    if ($teamnumber > 0) {
        echo "<table border=1>";
        echo "<tr><td colspan=3>Copy these accounts to distribute</td></tr>";
        echo "<tr><td>team_name<td>login_id</td><td>password</td></tr>";
        for ($i = 1; $i <= $teamnumber; $i++) 
        {
        		$pieceline = explode("\t", trim($pieces[$i - 1]));
        		//email
        		$email = "UnKnown";
        		//team name
        		if(isset($pieceline[0]) && strlen($pieceline[0])) $nick = mysql_real_escape_string(htmlspecialchars ($pieceline[0]));
            else $nick = "UnKnown";
            //school name
        		if(isset($pieceline[1]) && strlen($pieceline[1])) $school = mysql_real_escape_string(htmlspecialchars ($pieceline[1]));
        		else $school = "UnKnown";
        		//contestants
        		if(isset($pieceline[2]) && strlen($pieceline[2])) $contestants = mysql_real_escape_string(htmlspecialchars ($pieceline[2]));
        		else $contestants  = "UnKnown";
        		//contestants
        		if(isset($pieceline[3]) && strlen($pieceline[3])) $coach = mysql_real_escape_string(htmlspecialchars ($pieceline[3]));
        		else $coach  = "UnKnown";
        		//passwd
        		if(isset($pieceline[4]) && strlen($pieceline[4])) $password = $pieceline[4];
        		else $password = strtoupper(substr(MD5($user_id . rand(0, 9999999)), 0, 10));
        		//userid
        		if(isset($pieceline[5]) && strlen($pieceline[5]))
        		{
        			$user_id = $pieceline[4];
        			if (!is_valid_user_name($user_id)) 
					    {
					         $user_id  = $prefix . $i;
					    }
					  }
        		else $user_id  = $prefix . $i;
        		
        		
            echo "<tr><td>$nick<td>$user_id</td><td>$password</td></tr>";
            
            $password = pwGen($password);
            
            $sql    = "INSERT INTO `users`(" . "`user_id`,`email`,`ip`,`accesstime`,`password`,`reg_time`,`nick`,`school`,`coach`,`contestants`)" . "VALUES('" . $user_id . "','" . $email . "','" . $_SERVER['REMOTE_ADDR'] . "',NOW(),'" . $password . "',NOW(),'" . $nick . "','" . $school . "','" . $coach . "','" . $contestants . "')on DUPLICATE KEY UPDATE `email`='" . $email . "',`ip`='" . $_SERVER['REMOTE_ADDR'] . "',`accesstime`=NOW(),`password`='" . $password . "',`reg_time`=now(),nick='" . $nick . "',`school`='" . $school . "',`coach`='" . $coach . "',`contestants`='" . $contestants . "'";
            mysql_query($sql) or die(mysql_error());
        }
        echo "</table>";
        
        
    }
    
}
?>
<b>TeamGenerator:</b>
	
	<form action='team_generate.php' method=post>
	    Prefix:<input type='test' name='prefix' value='team'>
		Generate<input type=input name='teamnumber' value=50>Teams.
	<input type=submit value=Generate></br>
                格式说明:队名[tab]校名[tab]队员姓名[tab]教练/领队[tab]pass[tab]login_id</br>(所需条目前面的内容必须用非空字符代替位置，后面的则不需要。假设需要设置“队员姓名”，如果不需要“队员姓名”前面的“队名”，则“队名”位置必须用非空字符代替，而如果不需要后面的所有信息，则不输入即可。)</br><textarea name="ulist" rows="30" cols="60"><?php if (isset($ulist)) { echo $ulist; } ?></textarea>
		<?php require_once("../include/set_post_key.php");?>
	</form>


